package util.android.form.respuesta;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import util.android.form.ListViewFormAdapter;
import util.form.CuestionarioContext;
import util.form.pregunta.Friend;
import util.form.pregunta.Restriccion0;
import util.form.respuesta.RegistroImpl;

import android.widget.TableLayout;

public class ListViewInput 
extends AbstractInputAndroid0<ListViewInput>
{

	private final TableLayout viewGroup;
	private CuestionarioContext cuestionarioContext;
	private ListViewFormAdapter formAdapter;
	private final EditTextInputGroup editTextInputGroup;
	
	private final Map<String,ItemListView> mapItems;
	private final List<ItemListView> items;
	private Iterator<ItemListView> it;

	public ListViewInput(TableLayout viewGroup) {
		this.viewGroup = viewGroup;	
		this.editTextInputGroup = new EditTextInputGroup(this);
		mapItems = new HashMap<String, ItemListView>();
		items = new ArrayList<ItemListView>();		
	}
	
	@Override
	public TableLayout getView() {
		return viewGroup;
	}
	
	public ListViewInput setAdapter(
			CuestionarioContext cuestionarioContext, 
			ListViewFormAdapter formAdapter) throws Exception {
		this.cuestionarioContext = cuestionarioContext;
		this.formAdapter = formAdapter;
				
		RegistroImpl r = null;
		for(Map.Entry<String,ItemListView> e: mapItems.entrySet()) {			
			String key = e.getKey();
			System.out.println("KEY "+key);
			ItemListView it = e.getValue();
			if(r==null)
				r = it.getRegistroSinRelevancia();
			
			it.setRelevancia(formAdapter.getRelevancia(key));
			
			it.getCb().setChecked(formAdapter.getCompletado(key));
			if(it.isRelevante()) {
				EditTextInput in = editTextInputGroup.getEditTextInput(key);
				if(in!=null)
					in.setRelevancia(true);
			} 
			else {
				formAdapter.onSaltar(key,r);
			}
		}
				
		return this;
	}
	
	public final ListViewInput agregar(final String key, final String etiqueta) {
		final ItemListView it = new ItemListView(this, key, etiqueta);
		viewGroup.addView(it.getTr());
						
		mapItems.put(key, it);
		items.add(it);
		
		Friend.listViewInput(this, new Restriccion0() {			
			@Override
			public String validar() throws Exception {
				if(it.isRelevante() && !it.getCb().isChecked()) {
					return "Falta ingresar: '"+it.getTvEtiqueta().getText().toString()+"'";
				}
				return null;
			}
		});			
		return this;
	}
	
	

	public final ListViewInput agregarOtro(String key, String etiqueta, 
			EditTextInputCommand editTextCommand) throws Exception {
		agregar(key,etiqueta);		
		editTextInputGroup.agregarOtro(viewGroup, key, editTextCommand);
		return this;
	}

	@Override
	protected void soloLectura(boolean soloLectura) {
		
	}

	public ListViewFormAdapter getAdapter() {
		return formAdapter;
	}

	public CuestionarioContext getCuestionarioContext() {
		return cuestionarioContext;
	}

	public EditTextInput getEditTextInput(String key) {
		return editTextInputGroup.getEditTextInput(key);
	}

	public void apuntarItem(String key) throws Exception {
		it = items.iterator();
		while(it.hasNext()) {
			ItemListView item = it.next();			
			if(item.getKey().equals(key))
				break;
		}
	}

	public void abrirProximoItemRelevanteIncompleto() throws Exception {
		if(it!=null) {
			while(it.hasNext()) {
				ItemListView item = it.next();
				if(item.isRelevante() && !item.getCb().isChecked()) {
					item.mostrarDialog();
					break;
				}
			}	
		}
	}


	
	
}
